﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyLocalChurch.Models;
using MyLocalChurch.Models.KidsChurch;

namespace MyLocalChurch.Business
{
    public static class CheckInOutManager
    {
        public static CheckInOut GetUnCheckedOut(IEnumerable<CheckInOut> checkInOuts)
        {
            var checkIn = checkInOuts
                .Where(c => !c.CheckOutTimeStamp.HasValue)
                .OrderBy(c => c.CheckInTimeStamp)
                .LastOrDefault();

            if (checkIn != null && checkIn.CheckInTimeStamp.Date == DateTime.Today)
                return checkIn;

            return null;
        }

        public static CheckInOut GetCheckInForCheckOut(IEnumerable<CheckInOut> checkInOuts)
        {
            return checkInOuts.Where(cs => cs.CheckInTimeStamp.Date == DateTime.Today && !cs.CheckOutTimeStamp.HasValue)
                              .OrderBy(cs => cs.CheckInTimeStamp)
                              .LastOrDefault();
        }

        public static bool HasKidsForCheckOut(Guardian guardian)
        {
            return guardian.Kids.Any(k => GetCheckInForCheckOut(k.CheckInOuts) != null);
        }
    }
}